#include "view_others_device_info.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "datetime.h"
#include "device_type.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "font/num_font.h"
#include "heart_rate/view_heart_rate.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "platform_assert.h"
#include "prj_config.h"
#include "rtc.h"
#include "sport_his.h"
#include "times/view_time_0.h"
#include "view_common.h"
#include "view_others_device_info.inc"

#define BATTERY_BAR_WIDTH 42
#define BATTERY_BAR_HEIGHT 21
#define BATTERY_X 39
#define BATTERY_Y 50

void view_others_device_info_background(void)
{
    uint8_t battery_bar_end_x = BATTERY_X + (BATTERY_BAR_WIDTH * disp_ctrl.com_data.i_temp[4] / 100);
    uint8_t battery_bar_end_y = BATTERY_Y + BATTERY_BAR_HEIGHT;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 32, 44, FILE_IMG_other_battery);
    disp_draw_rect_color(BATTERY_X, BATTERY_Y, battery_bar_end_x, battery_bar_end_y, RGB565_COLOR_WHITE);
}

void view_others_device_info_battery_percent(void)
{
    int battery = disp_ctrl.com_data.i_temp[4];
    int length = get_num_digit(battery);
    draw_num_with_icon_screen(battery, length, 56, 94, 10, 2, img_view_heart_reat_battery_num_TAB, 15, img_view_heart_reat_percent, 0);
}

void view_others_device_info_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[4] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[6] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[6] %= 12;
            if (disp_ctrl.com_data.i_temp[6] == 0)
            {
                disp_ctrl.com_data.i_temp[6] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[7] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
    }
    show_time_0_ble(disp_ctrl.com_data.i_temp[5]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[4]);
    view_others_device_info_background();
    view_others_device_info_battery_percent();
    //		char buffer[20] = {0};
    //		uint16_t len = 16;
    //		memset(buffer,0,sizeof(buffer));
    //		sd_ble_gap_device_name_get((uint8_t*)buffer,&len);
    //		disp_show_user_string_middle(0,21,178,buffer,num_hex_12x28_get_bmp);
}

void view_others_device_info_detail_display(void)
{
    // uint8_t y_offset = 0;
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.auto_refresh_time = 500;
        memset(&disp_ctrl.com_data.uint8_temp[100], 0, 64);
        get_show_device_name(&disp_ctrl.com_data.uint8_temp[100], 32);
    }
    show_time_0_ble(disp_ctrl.com_data.i_temp[5]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9]);
}
